from sqlalchemy.orm import Session
from app.database import engine
from app.schemas.gift import Gift
from app.models.user import Gift as GiftModel, GiftRecord as RecordModel


class GiftServices:
    @staticmethod
    def index():
        with Session(engine) as session:
            all = session.query(GiftModel).all()
            return all

    @staticmethod
    def store(form: Gift):
        with Session(engine) as session:
            obj = GiftModel(**form.dict())
            session.add(obj)
            session.commit()
        return True

    @staticmethod
    def destroy(id: int):
        with Session(engine) as session:
            session.query(GiftModel).filter_by(id=id).delete()
            session.commit()
        return True

    @staticmethod
    def update(id: int, form: Gift):
        with Session(engine) as session:
            session.query(GiftModel).filter_by(id=id).update(form.dict())
            session.commit()
        return True

    @staticmethod
    def list_record(user):
        with Session(engine) as session:
            return session.query(RecordModel).filter_by(uid=user.id).all()

    @staticmethod
    def destroy_record(id: int):
        with Session(engine) as session:
            session.query(RecordModel).filter_by(id=id).delete()
            session.commit()
        return True
